﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using Maticsoft.Common;
using LTP.Accounts.Bus;
namespace Chizx.Web.apply
{
    public partial class Modify : Page
    {       

        		protected void Page_Load(object sender, EventArgs e)
        {
            if (null == Session["UserEntity"])
            {
                Response.Redirect("/e/Login.aspx");
            }
			if (!Page.IsPostBack)
			{
				int za_id = -1;
				if (Request.Params["id0"] != null && Request.Params["id0"].Trim() != "")
				{
					za_id=(Convert.ToInt32(Request.Params["id0"]));
				}
				string zu_id = "";
				if (Request.Params["id1"] != null && Request.Params["id1"].Trim() != "")
				{
					zu_id= Request.Params["id1"];
				}
				ShowInfo(za_id,zu_id);
			}
		}
			
	private void ShowInfo(int za_id,string zu_id)
	{
		Chizx.BLL.ApplyBll bll=new Chizx.BLL.ApplyBll();
		Chizx.Model.ApplyModel model=bll.GetModel(za_id);
		this.lblza_id.Text=model.za_id.ToString();
		this.lblzu_id.Text=model.zu_id;
        this.txtza_cid.Text = model.za_cid == 0 ? "自主实习" : model.za_cid.ToString();
		this.txtza_cname.Text=model.za_cname;
		this.txtza_caddress.Text=model.za_caddress;
		this.txtza_desc.Text=model.za_desc;
		this.txtza_createdate.Text=model.za_createdate.ToString();
        this.ddza_status.SelectedItem.Value = model.za_status == "a0" ? "未审核" : (model.za_status == "a1" ? "已通过" : (model.za_status == "a2" ? "已驳回" : "未知状态"));
	}

		public void btnSave_Click(object sender, EventArgs e)
		{
			
			string strErr="";
            string cid_this = this.txtza_cid.Text == "自主实习" ? "0" : this.txtza_cid.Text;
			if(this.txtza_cname.Text.Trim().Length==0)
			{
				strErr+="za_cname不能为空！\\n";	
			}
			if(this.txtza_caddress.Text.Trim().Length==0)
			{
				strErr+="za_caddress不能为空！\\n";	
			}
			if(this.txtza_desc.Text.Trim().Length==0)
			{
				strErr+="za_desc不能为空！\\n";	
			}
			if(!PageValidate.IsDateTime(txtza_createdate.Text))
			{
				strErr+="za_createdate格式错误！\\n";	
			}
			if(this.ddza_status.SelectedItem.Value.Trim().Length==0)
			{
				strErr+="za_status不能为空！\\n";	
			}
           
			if(strErr!="")
			{
				MessageBox.Show(this,strErr);
				return;
			}
			int za_id=int.Parse(this.lblza_id.Text);
			string zu_id=this.lblzu_id.Text;
			int za_cid=int.Parse(cid_this);
			string za_cname=this.txtza_cname.Text;
			string za_caddress=this.txtza_caddress.Text;
			string za_desc=this.txtza_desc.Text;
			DateTime za_createdate=DateTime.Parse(this.txtza_createdate.Text);
            string za_status = this.ddza_status.SelectedItem.Value == "未审核" ? "a0" : (this.ddza_status.SelectedItem.Value == "批准" ? "a1" : (this.ddza_status.SelectedItem.Value == "驳回" ? "a2" : "a3"));


			Chizx.Model.ApplyModel model=new Chizx.Model.ApplyModel();
			model.za_id=za_id;
			model.zu_id=zu_id;
			model.za_cid=za_cid;
			model.za_cname=za_cname;
			model.za_caddress=za_caddress;
			model.za_desc=za_desc;
			model.za_createdate=za_createdate;
			model.za_status=za_status;
            //写入系统日志
            if (Session["UserEntity"] == null)
            {
                Response.Redirect("/e/Login.aspx");
            }
            else
            {
                Chizx.BLL.ApplyBll bll = new Chizx.BLL.ApplyBll();
                bll.Update(model);
                string logtext = "修改实习申请";
                if (this.ddza_status.SelectedItem.Value == "a1")
                {
                    Chizx.BLL.UserBll ubb = new Chizx.BLL.UserBll();
                    Chizx.Model.UserModel sxmodel = ubb.GetModelByID(zu_id);
                    sxmodel.zu_direct = this.ddza_direct.SelectedItem.Value;
                    sxmodel.zu_status = "u1";
                    sxmodel.zu_roles = "SX";
                    ubb.Update(sxmodel);
                    logtext += "  并指定指导老师";
                    if(this.txtza_cid.Text != "自主实习"){//对企业招人数量减1，若等于需求人数，则改变状态
                        Chizx.BLL.CompanysBll cb = new Chizx.BLL.CompanysBll();
                        Chizx.Model.CompanysModel cm = new Chizx.Model.CompanysModel();
                        cm = cb.GetModel(int.Parse(cid_this));
                        cm.zcom_passnum += 1;
                        if (cm.zcom_passnum >= cm.zcom_neednum)
                        {
                            cm.zcom_status = "c1";
                        }
                        cb.Update(cm);
                    }
                }
                Chizx.Model.UserModel sbmmodel = (Chizx.Model.UserModel)Session["UserEntity"];
                Chizx.BLL.SystemLogBll sbll = new Chizx.BLL.SystemLogBll();
                Chizx.Model.SystemLogModel slm = new Chizx.Model.SystemLogModel();
                slm.zu_id = sbmmodel.zu_id;
                slm.zl_dowhen = DateTime.Now;
                slm.zl_dowhat = logtext;
                slm.zl_dotype = "3";//0:Test,1:Auth,2:Read,3:Write,4:Del,5:Inpor
                slm.zl_dodesc = "操作成功";
                sbll.Add(slm);
                if (this.ddza_status.SelectedItem.Value != "未审核")
                {
                    Chizx.Model.MessageModel mm = new Chizx.Model.MessageModel();
                    Chizx.BLL.MessageBll mb = new Chizx.BLL.MessageBll();
                    mm.zu_id = this.lblzu_id.Text;
                    mm.zmsg_topic = "实习申请审核已被 " + this.ddza_status.SelectedItem.Value;
                    mm.zmsg_context = "尊敬的用户 " + this.lblzu_id.Text + " ,您的实习申请已经被 " + this.ddza_status.SelectedItem.Value+"了,请留意平台的最新消息！";
                    mm.zmsg_createdate = DateTime.Now;
                    mm.zmsg_status = "m0";
                    mb.Add(mm);
                }
                
                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "list.aspx");
            }


		}


        public void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("list.aspx");
        }
    }
}
